import 'package:flutter/material.dart';
import 'package:mall/components/image_network.dart';
import 'package:mall/core/models/store_index.dart';
import 'package:mall/screens/product/product_page.dart';
import 'package:mall/screens/product/product_area_pg_screen.dart';
import 'package:mall/components/clock_countdown.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../constants.dart';

/// 拼购
class HomeGroup extends StatelessWidget {
  final List<Floor> group;
  final Function onEnd;

  const HomeGroup({
    Key key,
    @required this.group,
    @required this.onEnd,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 8.w,
      runSpacing: 8.w,
      children: List.generate(
        group.length,
        (index) => _buildFloorItem(context, group[index]),
      ),
    );
  }

  Widget _buildFloorItem(BuildContext context, Floor floor) {
    return GestureDetector(
      onTap: () => _onTapFloorItem(context, floor),
      child: Container(
        padding: EdgeInsets.all(4.w),
        width: ((375 - 8 * 3) ~/ 2).w,
        height: 130.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4.w),
        ),
        child: Column(
          children: [
            // 标题
            Align(
              alignment: Alignment.topLeft,
              child: Text(
                "${floor.title}",
                style: TextStyle(fontSize: 13.sp),
              ),
            ),
            SizedBox(height: 4.w),
            // 图片
            Wrap(
              spacing: 4.w,
              runSpacing: 4.w,
              children: List.generate(
                floor.productLinks.length,
                (index) => _buildItem(context, floor.productLinks[index]),
              ),
            ),
            Spacer(),
            // 倒计时
            Align(
              alignment: Alignment.bottomRight,
              child: ClockCountdown(
                endTime: floor.pinGouStartTime.add(Duration(seconds: floor.maxSeconds)),
                onEnd: onEnd,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _onTapFloorItem(BuildContext context, Floor floor) {
    Navigator.of(context).pushNamed(ProductAreaPgScreen.routeName, arguments: floor);
  }

  Widget _buildItem(BuildContext context, Link link) {
    return GestureDetector(
      onTap: () => _onTapItem(context, link),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(4.w),
        child: ImageNetwork(
          url: "${link.image}",
          width: ((((375 - 8 * 3) ~/ 2) - 4 * 3) ~/ 2).w,
          height: 80.w,
        ),
      ),
    );
  }

  void _onTapItem(BuildContext context, Link link) {
    final uri = Uri.tryParse("$api_base_url${link.link}");
    debugPrint("$uri");
    final mId = int.tryParse("${uri?.pathSegments?.last}");
    final r = int.tryParse("${uri?.queryParameters["r"]}");
    Navigator.of(context).pushNamed(
      ProductPage.routeName,
      arguments: ProductPageArgs(null, null, mId, r),
    );
  }
}
